package com.lanou.horo.zhuizhui.activity;


import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.horo.zhuizhui.R;
import com.lanou.horo.zhuizhui.fragment.CommunityPageFragment;
import com.lanou.horo.zhuizhui.fragment.HomePageFragment;
import com.lanou.horo.zhuizhui.fragment.PersonalPageFragment;
import com.lanou.horo.zhuizhui.fragment.PicturePageFragment;
import com.lanou.horo.zhuizhui.fragment.SearchPageFragment;
import com.lanou.horo.zhuizhui.utility.titleStyle.SystemBarTintManager;

public class MainActivity extends FragmentActivity {

    private TabHost tabHost;
    private FragmentManager manager;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);

        init();
        initFragment();

    }


    public void init() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);

            tintManager.setStatusBarTintResource(R.color.homepage_titleTv);
        }
        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();


        TabHost.TabSpec firsTabSpec = tabHost.newTabSpec("first");
        View firstTabView = getLayoutInflater().inflate(R.layout.tab_homebtn, null);
        firsTabSpec.setContent(R.id.view1);
        firsTabSpec.setIndicator(firstTabView);
        tabHost.addTab(firsTabSpec);

        TabHost.TabSpec secondSpec = tabHost.newTabSpec("second");
        View secondTabView = getLayoutInflater().inflate(R.layout.tab_daybtn,null);
        secondSpec.setContent(R.id.view2);
        secondSpec.setIndicator(secondTabView);
        tabHost.addTab(secondSpec);

        TabHost.TabSpec thirdSpec = tabHost.newTabSpec("three");
        View thirdTabView = getLayoutInflater().inflate(R.layout.tab_welfarebtn,null);
        thirdSpec.setContent(R.id.view3);
        thirdSpec.setIndicator(thirdTabView);
        tabHost.addTab(thirdSpec);

        TabHost.TabSpec fourSpec = tabHost.newTabSpec("four");
        View fourTabView = getLayoutInflater().inflate(R.layout.tab_srarchbtn,null);
        fourSpec.setContent(R.id.view4);
        fourSpec.setIndicator(fourTabView);
        tabHost.addTab(fourSpec);

        TabHost.TabSpec fiveSpec = tabHost.newTabSpec("five");
        View fiveTabView = getLayoutInflater().inflate(R.layout.tab_userinifbtn,null);
        fiveSpec.setContent(R.id.view5);
        fiveSpec.setIndicator(fiveTabView);
        tabHost.addTab(fiveSpec);
    }
    public void initFragment() {
        manager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        fragmentTransaction.replace(R.id.view1, new HomePageFragment());
        fragmentTransaction.replace(R.id.view2, new CommunityPageFragment());
        fragmentTransaction.replace(R.id.view3, new PicturePageFragment());
        fragmentTransaction.replace(R.id.view4, new SearchPageFragment());
        fragmentTransaction.replace(R.id.view5, new PersonalPageFragment());

        fragmentTransaction.commit();
    }
    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                showCustomToast("再按一次返回退出o",R.color.transparent);
//                Toast.makeText(getApplicationContext(), "再按一次返回退出o",
//                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    private void showCustomToast(String s, int image) {
        //获取LayoutInflater对象，该对象可以将布局文件转换成与之一致的view对象
        LayoutInflater inflater = getLayoutInflater();
        //将布局文件转换成相应的View对象
        View layout = inflater.inflate(R.layout.custome_toast_layout,
                (ViewGroup) findViewById(R.id.toast_layout_root));
        //从layout中按照id查找imageView对象
        ImageView imageView = (ImageView) layout.findViewById(R.id.ivForToast);
        //设置ImageView的图片
        imageView.setBackgroundResource(image);
        //从layout中按照id查找TextView对象
        TextView textView = (TextView) layout.findViewById(R.id.tvForToast);
        //设置TextView的text内容
        textView.setText(s);
        //实例化一个Toast对象
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 500);
        toast.setView(layout);
        toast.show();
    }
}
